﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using SmartSiteManagementSystem.IServices;
using SmartSiteManagementSystem.IRepository.Material;
using SmartSiteManagementSystem.Models.Material;
using System.Linq;

namespace SmartSiteManagementSystem.Services
{
    public class MaterialService : IMaterialService
    {

        private readonly IMaterialInfoRepository _materialInfoRepository;
        private readonly IMaterialCatalogInfoRepository _materialCatalogInfoRepository;
        public MaterialService(IMaterialInfoRepository materialInfoRepository
            , IMaterialCatalogInfoRepository materialCatalogInfoRepository)
        {
            _materialInfoRepository = materialInfoRepository;
            _materialCatalogInfoRepository = materialCatalogInfoRepository;

        }

        #region MaterialCatalogInfo

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int InsertMaterialCatalogInfo(MaterialCatalogInfo obj)
        {
            return _materialCatalogInfoRepository.Insert(obj);
        }


        /// <summary>
        /// 
        /// </summary>
        public IList<MaterialCatalogInfo> GetMaterialCatalogInfoList(Hashtable param)
        {
            if (param == null)
                param = new Hashtable();
            return _materialCatalogInfoRepository.GetList(param);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="catalogId"></param>
        /// <returns></returns>
        public MaterialCatalogInfo GetMaterialCatalogInfoById(int catalogId)
        {
            Hashtable param = new Hashtable();
            param.Add("catalog_id", catalogId);
            return _materialCatalogInfoRepository.Get(param);
        }


        /// <summary>
        /// 得到某分类下的分类列表
        /// </summary>
        /// <param name="cateId">父类id</param>
        /// <returns></returns>
        public IList<MaterialCatalogInfo> GetCatalogChildList(int cateId)
        {
            Hashtable param = new Hashtable();
            param.Add("is_deleted", "0");
            param.Add("parent_id", cateId);
            return _materialCatalogInfoRepository.GetList(param);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="catalogId"></param>
        /// <returns></returns>
        public IList<MaterialCatalogInfo> GetChildListStopC(int catalogId)
        {
            Hashtable param = new Hashtable();
            param.Add("is_deleted", "0");
            param.Add("is_stop", "0");
            param.Add("parent_id", catalogId);
            return _materialCatalogInfoRepository.GetList(param);
        }


        /// <summary>
        /// 得到分类Id集合下的分类列表 
        /// </summary>
        /// <param name="cateIds">父类id计划 用,号隔开</param>
        /// <returns></returns>
        public IList<MaterialCatalogInfo> GetCatalogChildList(string cateIds)
        {
            Hashtable param = new Hashtable();
            param.Add("is_deleted", "0");
            param.Add("is_stop", "0");
            param.Add("ParentIds", cateIds); //
            return _materialCatalogInfoRepository.GetList(param);
        }


        /// <summary>
        /// 根据子分类id获取父分类ID的字符
        /// (最多可以获取5级深度)
        /// </summary>
        /// <param name="catalogId"></param>
        /// <returns>返回格式如：4,5,8,10</returns>
        public string GetParentCatalogIds(int catalogId)
        {
            string resultId = string.Empty;

            MaterialCatalogInfo catalogObj = GetMaterialCatalogInfoById(catalogId);

            if (catalogObj != null && catalogObj.ParentId > 0)
            {
                resultId = catalogObj.ParentId.ToString();

                MaterialCatalogInfo pCatalogObj = GetMaterialCatalogInfoById(catalogObj.ParentId);
                if (pCatalogObj.ParentId > 0)
                {
                    resultId += "," + pCatalogObj.ParentId;

                    MaterialCatalogInfo ppCatalogObj = GetMaterialCatalogInfoById(pCatalogObj.ParentId);
                    if (ppCatalogObj.ParentId > 0)
                    {
                        resultId += "," + ppCatalogObj.ParentId;

                        MaterialCatalogInfo pppCatalogObj = GetMaterialCatalogInfoById(ppCatalogObj.ParentId);
                        if (pppCatalogObj.ParentId > 0)
                            resultId += "," + pppCatalogObj.ParentId;
                    }
                }
            }
            return resultId;
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="catalogId"></param>
        /// <returns></returns>
        public IList<MaterialCatalogInfo> GetParents(int catalogId)
        {
            Hashtable param = new Hashtable();
            param.Add("CatalogId", catalogId);
            return _materialCatalogInfoRepository.GetParents(param);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="catalogId"></param>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public bool DeleteMaterialCatalog(int catalogId, int parentId)
        {
            try
            {
                return _materialCatalogInfoRepository.Delete(catalogId, parentId);
            }
            catch (Exception ex)
            {
                
                return false;
            }
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool UpdateMaterialCatalogInfo(MaterialCatalogInfo obj)
        {
            return _materialCatalogInfoRepository.Update(obj);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public bool UpdateMaterialCatalogInfo(Hashtable param)
        {
            return _materialCatalogInfoRepository.Update(param);
        }



        #endregion

        #region MaterialInfo
        /// <summary>
        /// 
        /// </summary>
        /// <param name="catalogId"></param>
        /// <returns></returns>
        public int GetMaterialInfoCountByCatalogId(int catalogId)
        {
            Hashtable param = new Hashtable();
            param.Add("catalog_id", catalogId);
            param.Add("is_deleted", 0); 
            return _materialInfoRepository.GetCount(param);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public int GetMaterialInfoListCount(Hashtable param)
        {
            return _materialInfoRepository.GetCount(param);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="param"></param>
        /// <param name="orderBy"></param>
        /// <param name="first"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        public IList<MaterialInfo> GetMaterialInfoList(Hashtable param,string orderBy,int first,int rows)
        {
            return _materialInfoRepository.GetList(param, orderBy, first, rows);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="materialId"></param>
        /// <returns></returns>
        public MaterialInfo GetMaterialInfoById(int materialId)
        {
            Hashtable param = new Hashtable();
            param.Add("material_id", materialId);
            return _materialInfoRepository.Get(param);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="materialInfo"></param>
        /// <returns></returns>
        public bool UpdateMaterialInfo(MaterialInfo materialInfo)
        {
            bool back = _materialInfoRepository.Update(materialInfo);
            return back;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="materialInfo"></param>
        /// <returns></returns>
        public int InsertMaterialInfo(MaterialInfo materialInfo)
        {
            return _materialInfoRepository.Insert(materialInfo);
        }

        #endregion

    }
}
